package com.nineclock.auth.processor.impl;

import cn.hutool.core.util.StrUtil;
import com.nineclock.auth.entity.IntegrationAuthenticationEntity;
import com.nineclock.auth.processor.AuthenticationProcessor;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.system.dto.SysUserDTO;
import com.nineclock.system.feign.SysUserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 用户名/手机号-密码验证
*/

@Component
public class UsernameAndPasswordAuthenticationProcessor implements AuthenticationProcessor {

    @Autowired
    SysUserFeign sysUserFeign;

    @Override
    public SysUserDTO authenticate(IntegrationAuthenticationEntity entity) {
//        获取用户名密码、简况判断、封装
        String username = entity.getAuthParameter("username");
        String password = entity.getAuthParameter("password");

        if (username==null&&password==null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        SysUserDTO sysUserDTO = sysUserFeign.queryUser(username).getData();
        if(sysUserDTO == null){
            throw new NcException(ResponseEnum.INVALID_USERNAME_PASSWORD);
        }

        return sysUserDTO;
    }

    @Override
    public boolean support(IntegrationAuthenticationEntity entity) {
//        从请求中得到AuthType=null为用户名/手机号-密码验证
        return entity != null && StrUtil.isEmpty(entity.getAuthType());
    }
}
